package com.yarin.android.Examples_08_09;

/**
 * Created by Gallery on 13-9-13.
 */
public class MathMethod {

    public static String byte2HexStr(byte[] b) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        for (int n=0; n<b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            sb.append((stmp.length()==1)? "0"+stmp : stmp);
            sb.append(";");
        }
        return sb.toString().toUpperCase().trim();
    }

    public static int bytesToInt(byte[] bytes) {
        int addr = bytes[0] & 0xFF;
        addr |= ((bytes[1] << 8) & 0xFF00);
        addr |= ((bytes[2] << 16) & 0xFF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }

    public static int byteHEXtoInt(byte byteX) {
        byte[] targetBytes = new byte[4];
        targetBytes[0] = byteX;
        targetBytes[1] = 0x00;
        targetBytes[2] = 0x00;
        targetBytes[3] = 0x00;

        return MathMethod.bytesToInt(targetBytes);
    }
}
